package com.gxgy.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gxgy.base.BaseAllInterface;
import com.gxgy.entity.Student;
import com.gxgy.uilt.IfVersionException;


/**
 * @description: 学生表 接口
 * @author: Qin Fengang
 * @date: 2021-11-19
 */
@Controller
public class StudentController extends BaseAllInterface{


/**
 * @description: 学生 查询所有 并回显
 * @author: Qin Fengang
 * @date: 2021-11-19
 */
	@RequestMapping("/studentList")
	public String student(Model model) {
		List<Student> student = studentService.queryAllStudent();
		model.addAttribute("student",student);
		return "page/student/student";
	}
	
	/**
	 * @description: 学生 新增页面
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@RequestMapping("/student")
	public String studentAdd() {
		return "page/student/studentAdd";
	}
	/**
	 * @description: 学生 新增功能
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@RequestMapping("/doStudentAdd")
	public String doStudentAdd(String studentNumber, String studentName, String studentSex, Integer studentAge,
			String studentYear, String studentClass, String createTime,Model model) {
		if (studentNumber.isEmpty()) {
			model.addAttribute("msg","学号不能为空");
			return "page/student/studentAdd";
		}
		if (studentName.isEmpty()) {
			model.addAttribute("msg","学生姓名不能为空");
			return "page/student/studentAdd";
		}
		if (studentSex.isEmpty()) {
			model.addAttribute("msg","学生性别不能为空");
			return "page/student/studentAdd";
		}
		if (studentAge == null) {
			model.addAttribute("msg","学生年龄不能为空");
			return "page/student/studentAdd";
		}
		if (studentYear.isEmpty()) {
			model.addAttribute("msg","学生年级不能为空");
			return "page/student/studentAdd";
		}
		if (studentClass.isEmpty()) {
			model.addAttribute("msg","学生班级不能为空");
			return "page/student/studentAdd";
		}if (createTime.isEmpty()) {
			model.addAttribute("msg","创建时间不能为空");
			return "page/student/studentAdd";
		}
		Student student = studentService.queryByNumber(studentNumber);
		if (student != null) {
			model.addAttribute("msg","该学号已存在");
			return "page/student/studentAdd";
		}
		studentService.addStudent(studentNumber, studentName, studentSex, studentAge, studentYear, studentClass, createTime, model);
		return "redirect:studentList"; 
	}
	
	/**
	 * @description: 学生 编辑页
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@RequestMapping("/studentEdit")
	public String studentEdit(String id,Model model) {
		Student student = studentService.queryById(id);
		model.addAttribute("student",student);
		return "page/student/studentEdit";
		
	}
	
	/**
	 * @description: 学生 编辑功能
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@RequestMapping("/doStudentEdit")
	private String doStudentEdit(String id,String studentNumber, String studentName, String studentSex, Integer studentAge,
			String studentYear, String studentClass, String createTime,Model model) {
		if (studentNumber.isEmpty()) {
			model.addAttribute("msg","学号不能为空");
			return "page/student/studentEdit";
		}
		if (studentName.isEmpty()) {
			model.addAttribute("msg","学生姓名不能为空");
			return "page/student/studentEdit";
		}
		if (studentSex.isEmpty()) {
			model.addAttribute("msg","学生性别不能为空");
			return "page/student/studentEdit";
		}
		if (studentAge == null) {
			model.addAttribute("msg","学生年龄不能为空");
			return "page/student/studentEdit";
		}
		if (studentYear.isEmpty()) {
			model.addAttribute("msg","学生年级不能为空");
			return "page/student/studentEdit";
		}
		if (studentClass.isEmpty()) {
			model.addAttribute("msg","学生班级不能为空");
			return "page/student/studentEdit";
		}if (createTime.isEmpty()) {
			model.addAttribute("msg","创建时间不能为空");
			return "page/student/studentEdit";
		}
		Student studentByNumber = studentService.queryByNumber(studentNumber);
		if (studentByNumber != null) {
			model.addAttribute("msg","该学号已存在");
			return "page/student/studentEdit";
		}
		studentService.updateStudent(id, studentNumber, studentName, studentSex, studentAge, studentYear, studentClass, createTime, model);
		return "redirect:studentList";
		
	}
}
